/*
 * @Author: AskMeWhy
 * @Date: 2020-12-03 09:57:01
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-01-28 16:58:33
 */

import Api from "services/index";
import { merge, getBreadCrumbsName } from "@/utils";
import { STATIC_URL, API_DEFAULT_CONFIG } from "services/config";
import { USER_LEVEL, getUserLevel, TASK_MODULE_STATUS, getTaskModuleStatus } from "./core/state";

export default (Vue: any) => {
    Object.defineProperty(Vue.prototype, "$server", {
        get() {
            return Api.server;
        }
    });
    Object.defineProperty(Vue.prototype, "$info", {
        get() {
            return Api.info;
        }
    });
    Object.defineProperty(Vue.prototype, "$staticUrl", {
        get() {
            return STATIC_URL;
        }
    });
    Object.defineProperty(Vue.prototype, "$wsUrl", {
        get() {
            return API_DEFAULT_CONFIG.SocketUrl;
        }
    });
    /**
     * 将传入的Key值转换为对应的name值
     * @Author   陈龙
     * @DateTime 2018-08-28
     * @version  [version]
     * @param    {Object}   options
     * {
     *    type: '', //需要比较的值
     *    list: [], //需要比较的数据源,必须是数组
     *    key: 'value', //需要比较的值对应的key，默认为val
     *    name: 'name' //比较成功后返回对应key的值
     * }
     * @return   {[type]}   返回的传入name对应的值;
     */
    Vue.prototype.$key2Name = (
        options: any,
        type?: string
    ): object | string => {
        const key2Name = (options: any) => {
            let opt = { type: "", list: [], key: "value", name: "label" };
            opt = merge(true, opt, options);
            const { type, list, key: _key, name: _name } = opt;
            const res: any = [];
            const resObj: any = [];
            let _types: any = [];
            const typeIsArr = Array.isArray(type);
            if (!Array.isArray(list)) {
                return typeIsArr ? res : "";
            }
            _types = typeIsArr ? type : [type];
            _types.forEach((item: any) => {
                const _index = list.findIndex(
                    // eslint-disable-next-line no-void
                    cur => cur[_key] !== void 0 && cur[_key] === item
                );
                if (_index !== -1) {
                    resObj.push(list[_index]);
                    res.push(list[_index][_name]);
                }
            });
            return {
                res: typeIsArr ? res : res[0] || "",
                resObj: typeIsArr ? resObj : resObj[0] || {}
            };
        };
        const res = key2Name(options);
        return type && type === "obj" ? res.resObj : res.res;
    };
    const _getOnlyKey = (list: any, keyTag?: string): string => {
        const _keyTag = keyTag || "_key";
        const key = Math.random();
        const _index = list.findIndex((item: any) => item[_keyTag] === key);
        if (_index === -1) {
            return key + "";
        } else {
            return _getOnlyKey(list, _keyTag);
        }
    };
    Vue.prototype.$getOnlyKey = _getOnlyKey;
    Vue.prototype.$USER_LEVEL = USER_LEVEL;
    Vue.prototype.$getUserLevel = getUserLevel;
    Vue.prototype.$TASK_MODULE_STATUS = TASK_MODULE_STATUS;
    Vue.prototype.$getTaskModuleStatus = getTaskModuleStatus;
    Vue.prototype.$getBreadCrumbsName = getBreadCrumbsName;
};
